<template>
  <div class="table-wrap" v-loading="loading">
    <div class="search_header">
      <div class="search_header_left">
        <el-button
          icon="el-icon-plus"
          @click="addProject"
          v-has="'commercialHouse:add'"
          >新增</el-button
        >
        <el-button
          icon="el-icon-download"
          @click="downloadFile"
          v-has="'commercialHouse:export'"
          >下载数据</el-button
        >
        <el-button
          icon="el-icon-refresh"
          @click="trackProject"
          v-has="'commercialHouse:export'"
          >进度填报</el-button
        >
      </div>

      <div class="search_header_right">
        <el-input
          placeholder="请输入名称"
          v-model="parameter.name"
          @keydown.enter.native="search"
          clearable
          @clear="search"
        >
          <span
            @click="search"
            slot="suffix"
            style="display: inline-block"
          >
            <Icon
              title="搜索"
              name="sousuo"
              color="#A7ADB5"
              font-size="20"
            />
          </span>
        </el-input>
      </div>
    </div>
    <el-table
      :data="projectList"
      :header-cell-style="{
        background: '#F2F6FB',
        color: ' #6B767D'
      }"
      :row-class-name="tableRowClassName"
    >
      <el-table-column
        label="序号"
        align="center"
        show-overflow-tooltip
        width="80"
      >
        <template slot-scope="scope">
          {{ scope.$index + 1 }}
        </template>
      </el-table-column>
      <el-table-column
        label="所属项目(园区)"
        prop="parkName"
        show-overflow-tooltip
      />
      <el-table-column
        label="户型"
        prop="type"
        show-overflow-tooltip
      />
      <el-table-column
        label="户型面积(平方米)"
        prop="area"
        show-overflow-tooltip
      />
      <el-table-column
        label="户型总套数"
        prop="number"
        show-overflow-tooltip
      />
      <el-table-column
        label="剩余套数"
        prop="remainValue"
        show-overflow-tooltip
      />
      <el-table-column
        label="操作"
        show-overflow-tooltip
        width="240"
      >
        <template slot-scope="scope">
          <el-button
            @click="viewProject(scope.row)"
            type="text"
            style="color: #00baff; font-weight: 400"
            >查看</el-button
          >
          <el-button
            @click="editProject(scope.row)"
            type="text"
            style="color: #00baff; font-weight: 400"
            v-has="'commercialHouse:edit'"
            >编辑</el-button
          >
          <el-button
            @click="deleteProject(scope.row)"
            type="text"
            style="color: #ff5b5b; font-weight: 400"
            v-has="'commercialHouse:delete'"
            >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="table-pagination" v-if="total">
      <el-pagination
        background
        layout="prev, pager, next"
        :page-size="parameter.pageSize"
        @current-change="pageChange"
        :total="total"
        :current-page="parameter.pageIndex"
      >
      </el-pagination>
    </div>

    <AddHouseDialog
      :visible.sync="showDialog"
      :title="title"
      :currentData="currentData"
      :isDetail="isDetail"
      @confirm="handleSubmit"
    />
  </div>
</template>

<script>
import {
  addCommercialHouseInfo,
  queryCommercialHouseInfo,
  deleteCommercialHouseInfo,
  downloadCommercialHouseInfo
} from '@/api/dataManagement'
import Icon from '@/components/icon'
import AddHouseDialog from './components/addHouseDialog.vue'
import { downloadBlobFile } from '@/utils/download'
import listKeepAliveMixin from '@/mixins/listKeepAliveMixin'

export default {
  name: 'commercialHouseList',
  mixins: [listKeepAliveMixin],
  components: {
    Icon,
    AddHouseDialog
  },
  data() {
    return {
      projectList: [],
      total: '',
      parameter: {
        name: '',
        pageIndex: 1,
        pageSize: 10
      },
      projectTypeOptions: [],
      governProjectOptions: [
        {
          dictValue: '政府投资',
          dictText: '政府投资'
        },
        {
          dictValue: '社会投资',
          dictText: '社会投资'
        }
      ],
      loading: false,
      showDialog: false,
      isDetail: false,
      currentData: {
        parkName: '',
        area: '',
        number: '',
        remainValue: ''
      },
      title: ''
    }
  },
  mounted() {
    // this.initOptions()
    this.getList()
  },
  methods: {
    // async initOptions() {
    //   const data = await getDictOptions('zhcy_xmlx')
    //   this.projectTypeOptions = data
    // },

    search() {
      this.parameter.name =
        this.parameter.name?.trim?.() || ''
      this.parameter.pageIndex = 1
      this.getList()
    },

    pageChange(page) {
      this.parameter.pageIndex = page
      this.getList()
    },

    // 获取用户列表
    getList() {
      this.loading = true
      queryCommercialHouseInfo(this.parameter).then(
        (res) => {
          if (res.success) {
            this.total = res.data.count
            this.projectList = res.data.list
          }
          this.loading = false
        }
      )
    },

    // 表格高亮
    tableRowClassName({ rowIndex }) {
      if (rowIndex % 2 === 1) {
        return 'light'
      } else {
        return ''
      }
    },

    addProject() {
      this.showDialog = true
      this.title = '新增'
      this.isDetail = false
      this.currentData = {
        parkName: '',
        area: '',
        number: '',
        remainValue: ''
      }
    },

    // 编辑
    editProject(row) {
      this.showDialog = true
      this.title = '编辑'
      this.currentData = row
      this.isDetail = false
    },
    // 项目查看
    viewProject(row) {
      this.showDialog = true
      this.title = '查看'
      this.currentData = row
      this.isDetail = true
    },

    // 删除项目
    deleteProject(row) {
      this.$confirm(
        '此操作将永久删除该商品房，是否继续?',
        '提示',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }
      )
        .then(() => {
          deleteCommercialHouseInfo(row.pkid).then(
            (res) => {
              if (res.success) {
                this.$message.success('删除成功')
                if (
                  this.total % 10 === 1 &&
                  this.parameter.pageIndex ===
                    parseInt(this.total / 10) + 1 &&
                  this.parameter.pageIndex !== 1
                ) {
                  this.parameter.pageIndex -= 1
                }
                this.getList()
              } else {
                this.$message.error(res.message)
              }
            }
          )
        })
        .catch((error) => {
          this.$message.info('已取消删除')
        })
    },
    downloadFile() {
      this.btnLoading = true
      downloadCommercialHouseInfo(this.parameter)
        .then((res) => {
          downloadBlobFile(res, '商品房信息表.xlsx')
        })
        .catch((err) => {
          this.$message.error('下载失败')
        })
        .finally(() => {
          this.btnLoading = false
        })
    },

    handleSubmit(data) {
      addCommercialHouseInfo(data).then((res) => {
        if (res.success) {
          this.$message.success(
            data.pkid ? '编辑成功' : '保存成功'
          )
          this.getList()
        } else {
          this.$message.error(res.message)
        }
      })
    },

    trackProject() {
      this.$router.push({
        path: '/dataManagement/carrierReduction/commercialHouse/process',
        query: {
          title: '进度填报'
        }
      })
    }
  }
}
</script>

<style scoped lang="scss">
@import '~@/assets/css/table.scss';
</style>
